﻿Imports DataObject
Imports BusinessObject
Imports Microsoft.ApplicationBlocks.Data

Public Class frmViTriCongViecDetail

    Private ptyViTriCongViec As New ViTriCongViec
    Private oViTriCongViec As New BusViTriCongViec
    Private oFunction As New hDCommonFunction

    Private Sub frmViTriCongViecDetail_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        If (Me.eActionMode = EditMode.AddNew) Then
            Me.Text = "Thêm mới vị trí công việc"
        Else
            Me.Text = "Sửa vị trí công việc"
        End If
        LoadAllCombo()
        txtMaViTri.Focus()
        DataBindingToControl(GroupBox1)
    End Sub

    Public Overrides Function ValidateData() As Boolean
        If txtMaViTri.Text = "" Then
            MessageBox.Show("Mã vị trí công việc không được để trống", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error)
            txtMaViTri.Focus()
            Return False
        End If
        If txtTenViTri.Text = "" Then
            MessageBox.Show("Tên vị trí công việc không được để trống", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error)
            txtTenViTri.Focus()
            Return False
        End If
        Return True
    End Function

    Protected Overrides Function SaveDictionary() As Boolean
        Dim bResult As Boolean = False
        Try
            If ((Not cboChucDanh.Value Is Nothing) Or (cboChucDanh.Text.Equals("") = False)) Then
                ptyViTriCongViec.SoChucDanh = cboChucDanh.Value
            Else
                ptyViTriCongViec.SoChucDanh = Nothing
            End If

            If ((Not cboNhomViTri.Value Is Nothing) Or (cboNhomViTri.Text.Equals("") = False)) Then
                ptyViTriCongViec.SoNhomViTri = cboNhomViTri.Value
            Else
                ptyViTriCongViec.SoNhomViTri = Nothing
            End If

            ptyViTriCongViec.MaViTriCV = txtMaViTri.Text
            ptyViTriCongViec.TenViTriCV = txtTenViTri.Text
            ptyViTriCongViec.DienGiai = txtDienGiai.Text
            ptyViTriCongViec.HoatDong = chkHoatDong.CheckedValue

            If (eActionMode = EditMode.AddNew) Then
                oViTriCongViec.InsertViTriCongViec(sConn, ptyViTriCongViec)
            ElseIf (eActionMode = EditMode.Edit) Then
                ptyViTriCongViec.SoViTriCV = RowDetail.Item("SoViTriCV")
                oViTriCongViec.UpdateViTriCongViec(sConn, ptyViTriCongViec)
            End If

            bResult = True
        Catch ex As Exception
            MessageBox.Show(ex.ToString, "Thông báo lỗi", MessageBoxButtons.OK, MessageBoxIcon.Error)
            bResult = False
        End Try
        Return bResult
    End Function

    Private Sub LoadAllCombo()
        Dim ds As New DataSet
        ds.Clear()
        If (Not ds.Tables("ChucDanh") Is Nothing) Then
            ds.Tables("ChucDanh").Rows.Clear()
        End If
        SqlHelper.FillDataset(sConn, CommandType.StoredProcedure, "Proc_Get_ChucDanh_By_HoatDong", ds, New String() {"ChucDanh"})
        With cboChucDanh
            .DataSource = ds.Tables("ChucDanh")
            .DisplayMember = "TenChucDanh"
            .ValueMember = "SoChucDanh"
            .DisplayLayout.Load(PathLayout & "cboChucDanh.lyt")
        End With

        If (Not ds.Tables("NhomViTri") Is Nothing) Then
            ds.Tables("NhomViTri").Rows.Clear()
        End If
        SqlHelper.FillDataset(sConn, CommandType.StoredProcedure, "Proc_Get_NhomViTri_By_HoatDong", ds, New String() {"NhomViTri"})
        With cboNhomViTri
            .DataSource = ds.Tables("NhomViTri")
            .DisplayMember = "TenNhomViTri"
            .ValueMember = "SoNhomViTri"
            .DisplayLayout.Load(PathLayout & "cboNhomViTri.lyt")
        End With
    End Sub

    Private Sub cboChucDanh_ItemNotInList(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinEditors.ValidationErrorEventArgs) Handles cboChucDanh.ItemNotInList
        cboChucDanh.Text = ""
    End Sub

    Private Sub cboNhomViTri_ItemNotInList(ByVal sender As Object, ByVal e As Infragistics.Win.UltraWinEditors.ValidationErrorEventArgs) Handles cboNhomViTri.ItemNotInList
        cboNhomViTri.Text = ""
    End Sub

    Private Sub cboNhomViTri_EditorButtonClick(ByVal sender As System.Object, ByVal e As Infragistics.Win.UltraWinEditors.EditorButtonEventArgs) Handles cboNhomViTri.EditorButtonClick
        Dim oRow As DataRow
        Dim ds As New DataSet
        Dim f As New frmNhomViTriDetail
        Dim ListBindingSource As New System.Windows.Forms.BindingSource
        SqlHelper.FillDataset(sConn, CommandType.StoredProcedure, "Get_All_NhomViTris", ds, New String() {"NhomViTri"})
        oRow = ds.Tables("NhomViTri").NewRow
        With oRow
            .Item("MaNhomViTri") = ""
            .Item("TenNhomViTri") = ""
            .Item("HoatDong") = True
        End With
        ds.Tables("NhomViTri").Rows.Add(oRow)
        ListBindingSource.DataSource = ds.Tables("NhomViTri")
        ListBindingSource.MoveLast()
        With f
            .ActionMode = EditMode.AddNew
            .BindingSource = ListBindingSource
            .RowDetail = oRow
            .ShowDialog()
        End With
        LoadAllCombo()
    End Sub

    Private Sub cboChucDanh_EditorButtonClick(ByVal sender As System.Object, ByVal e As Infragistics.Win.UltraWinEditors.EditorButtonEventArgs) Handles cboChucDanh.EditorButtonClick
        Dim oRow As DataRow
        Dim ds As New DataSet
        Dim f As New frmChucDanhDetail
        Dim ListBindingSource As New System.Windows.Forms.BindingSource
        SqlHelper.FillDataset(sConn, CommandType.StoredProcedure, "Get_All_ChucDanhs", ds, New String() {"ChucDanh"})
        oRow = ds.Tables("ChucDanh").NewRow
        With oRow
            .Item("MaChucDanh") = ""
            .Item("TenChucDanh") = ""
            .Item("HoatDong") = True
        End With
        ds.Tables("ChucDanh").Rows.Add(oRow)
        ListBindingSource.DataSource = ds.Tables("ChucDanh")
        ListBindingSource.MoveLast()
        With f
            .ActionMode = EditMode.AddNew
            .BindingSource = ListBindingSource
            .RowDetail = oRow
            .ShowDialog()
        End With
        LoadAllCombo()
    End Sub

End Class
